﻿using HuiZe.education.Model;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// PART_MyShare_Item.xaml 的交互逻辑
    /// </summary>
    public partial class PART_MyShare_Item : UserControl
    {
        public resShareList Data
        {
            get { return (resShareList)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(resShareList), typeof(PART_MyShare_Item), new PropertyMetadata(null,(sender,e)=> 
            {
                PART_MyShare_Item _item = (PART_MyShare_Item)sender;
                resShareList _data = null;
                if (e.NewValue != null && e.NewValue is resShareList)
                    _data = (resShareList)e.NewValue;

                _item.Load(_data);
            }));

        public Action<resShareList> SelectedCallback { get; set; }
        public Action<resShareList> DownloadCallback { get; set; }
        public Action<resShareList> ReferenceCallback { get; set; }

        public PART_MyShare_Item()
        {
            InitializeComponent();
            PART_Download.MouseLeftButtonUp += PART_Download_MouseLeftButtonUp;
            PART_Reference.MouseLeftButtonUp += PART_Reference_MouseLeftButtonUp;
            PART_Background.MouseLeftButtonUp += PART_Background_MouseLeftButtonUp;
        }

        void PART_Background_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (SelectedCallback != null)
                SelectedCallback(this.Data);
        }

        void PART_Reference_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (ReferenceCallback != null)
                ReferenceCallback(this.Data);
        }

        void PART_Download_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (DownloadCallback != null)
                DownloadCallback(this.Data);
        }

        public void Load(resShareList _data)
        {
            if (_data == null) return;

            //BitmapImage _bmp = new BitmapImage();
            //_bmp.CacheOption = BitmapCacheOption.OnLoad;
            //_bmp.BeginInit();
            //_bmp.UriSource = new Uri(_data.image_cover, UriKind.RelativeOrAbsolute);
            //_bmp.EndInit();

            //PART_Icon.ImageSource = _bmp;

            PART_Title.Text = _data.title;
           // PART_CatName.Text = _data.cat_name;
            PART_UserName.Text = _data.nickname;
            PART_Count.Text = _data.number.ToString();
            PART_Time.Text = _data.createtime_text;
        }

        public void Load_Image()
        {
            if (Data == null || string.IsNullOrEmpty(Data.image_cover)) return;

            Load_Image(Data.image_cover);
        }

        void Load_Image(string _url)
        {
            using (var _client = new System.Net.WebClient())
            {
                _client.Proxy = null;
                _client.DownloadDataCompleted += (s, e) =>
                {
                    if (e.Result != null && e.Result.Length > 0)
                    {
                        try
                        {
                            BitmapImage _bmp = new BitmapImage();
                            _bmp.CacheOption = BitmapCacheOption.OnLoad;
                            _bmp.BeginInit();
                            _bmp.StreamSource = new System.IO.MemoryStream(e.Result);
                            _bmp.EndInit();

                            PART_Icon.ImageSource = _bmp;
                        }
                        catch (Exception ex)
                        {
                            App.Log(ex);
                        }
                    }
                };
                _client.DownloadDataAsync(new Uri(_url, UriKind.RelativeOrAbsolute));
            }
        }
    }
}
